package cn.agiledata.bank.audit.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;

import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.AuditOperandType;
import cn.agiledata.bank.hibernateBean.AuditRule;
import com.vnv.infrastructure.logic.IPBHibernate3BaseMgr;

/**
 * @author 马贤波
 * 
 */
public class AuditRuleDaoImpl extends IPBHibernate3BaseMgr implements
        IAuditRuleDao {

    public AuditRule getValidAuditRule(Long accountId, AuditOperandType type) {
        String hql = "from AuditRule ar where ar.auditOperandType=? and ar.valid="
                + Constant.VALID_YES;
        Object[] params;
        if (accountId == null) {
            hql += " and ar.account is null";
            params = new Object[] { type };
        } else {
            hql += " and ar.account.id=?";
            params = new Object[] { type, accountId };
        }
        HibernateTemplate ht = this.getHibernateTemplate();
        int maxResults = ht.getMaxResults();
        ht.setMaxResults(1);
        List resultList = ht.find(hql, params);
        ht.setMaxResults(maxResults);
        return resultList.size() == 0 ? null : (AuditRule) resultList.get(0);
    }

    public AuditRule getValidAuditRuleExceptFor(Account account,
            AuditOperandType type, Long auditRuleId) {
        String hql = "from AuditRule ar where ar.auditOperandType=? and ar.valid="
                + Constant.VALID_YES;
        List params = new ArrayList();
        params.add(type);
        if (account == null) {
            hql += " and ar.account is null";
        } else {
            hql += " and ar.account=?";
            params.add(account);
        }
        if (auditRuleId != null) {
            hql += " and ar.id<>?";
            params.add(auditRuleId);
        }
        HibernateTemplate ht = this.getHibernateTemplate();
        int maxResults = ht.getMaxResults();
        ht.setMaxResults(1);
        List resultList = ht.find(hql, params.toArray(new Object[0]));
        ht.setMaxResults(maxResults);
        return resultList.size() == 0 ? null : (AuditRule) resultList.get(0);
    }

    public AuditRule[] getAuditRules(boolean systemRule, Long accountId,
            Integer valid, Boolean updatable) {
        String hql = "from AuditRule ar where ar.auditOperandType.systemType=? ";
        List params = new ArrayList();
        params.add(new Boolean(systemRule));
        if (accountId == null) {
            hql += "and ar.account is null ";
        } else {
            hql += "and ar.account.id=? ";
            params.add(accountId);
        }
        if (valid != null) {
            hql += "and ar.valid=? ";
            params.add(valid);
        }
        if (updatable != null) {
            hql += "and ar.auditOperandType.ruleUpdatable=? ";
            params.add(updatable);
        }
        List resultList = this.getHibernateTemplate().find(hql,
                params.toArray());
        return (AuditRule[]) resultList.toArray(new AuditRule[0]);
    }

    public AuditRule getAuditRuleById(long id) {
        return (AuditRule) this.getHibernateTemplate().get(AuditRule.class,
                new Long(id));
    }

    public void saveOrUpdateAuditRule(AuditRule auditRule) {
        this.getHibernateTemplate().saveOrUpdate(auditRule);
        this.getHibernateTemplate().flush();
    }

    public void deleteAuditRule(AuditRule auditRule) {
        this.getHibernateTemplate().delete(auditRule);
        this.getHibernateTemplate().flush();
    }

    public AuditRule getAuditRuleByName(boolean systemRule, Account account,
            String name) {
        String hql = "from AuditRule ar where ar.auditOperandType.systemType=? and ar.name=? ";
        Object[] params;
        if (account == null) {
            hql += "and ar.account is null";
            params = new Object[] { new Boolean(systemRule), name };
        } else {
            hql += "and ar.account=?";
            params = new Object[] { new Boolean(systemRule), name, account };
        }
        HibernateTemplate ht = this.getHibernateTemplate();
        int maxResults = ht.getMaxResults();
        ht.setMaxResults(1);
        List resultList = ht.find(hql, params);
        ht.setMaxResults(maxResults);
        return resultList.size() == 0 ? null : (AuditRule) resultList.get(0);
    }

}
